package com.lancoo.znbkxx.teacher.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.PopupWindow
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.EvenItemDecoration
import com.lancoo.znbkxx.teacher.adapter.SpacesItemDecoration
import com.lancoo.znbkxx.teacher.adapter.SwitchStudentAdapter
import com.lancoo.znbkxx.teacher.model.data.ValidStu
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.fmt_stu_progress.*
import kotlinx.android.synthetic.main.view_switch_student.view.*

/**
 * Created by wealive on 2021/3/11.
 *
 *
 */
class SwitchStudentWindow(val context: Context, val data: List<ValidStu>) : PopupWindow() {
    var adapter: SwitchStudentAdapter

    init {
        val contentview = LayoutInflater.from(context).inflate(R.layout.view_switch_student, null)
        adapter = SwitchStudentAdapter(data)
        adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
            for (i in data.indices) {
                (adapter.getViewByPosition(contentView.rv_stu, i,
                        R.id.tv) as CheckBox).isChecked = false
                data[i].Checked = false
            }
            (view as CheckBox).isChecked = true
            data[position].Checked = true
        }
        contentview.rv_stu.layoutManager = GridLayoutManager(context, 4)
        val hashMap = hashMapOf<String, Int>()
        hashMap[SpacesItemDecoration.RIGHT_SPACE] = DensityUtils.dp2px(context, 5f)
        hashMap[SpacesItemDecoration.TOP_SPACE] = DensityUtils.dp2px(context, 5f)
        contentview.rv_stu.addItemDecoration(SpacesItemDecoration(hashMap))
        contentview.rv_stu.adapter = adapter
        this.contentView = contentview
        animationStyle = R.style.PopupWindow
        this.width = WindowManager.LayoutParams.MATCH_PARENT
        this.height = WindowManager.LayoutParams.WRAP_CONTENT
        this.isOutsideTouchable = true
        this.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        this.isFocusable = true
        update()
    }
}